import { Component } from '@angular/core';
import { HttpService } from '../../../service/http.service';
import { NzMessageService } from 'ng-zorro-antd/message';

@Component({
  selector: 'app-personal',
  templateUrl: './personal.component.html',
  styleUrls: ['./personal.component.css'],
})
export class PersonalComponent {
  public userInfo: any = {
    id: 0,
    name: '',
    lastLogin: '',
    email: '',
    verificationCode: '',
    emailActivated: 0, //0表示未绑定邮箱，1表示绑定未激活，2表示激活
    lastLoginIP: '',
  };
  public resetInfo: any = {
    oldPassword: '',
    newPassword: '',
    confirmPassword: '',
  };
  public userDetails: any = {};
  public showUpdateFlag = false;
  public showVerificationFlag = false;
  public sendButton = false;
  public count = 0;
  public timer: any;
  public countText = '';

  constructor(
    private httpService: HttpService,
    private modalService: NzMessageService,
  ) {}

  ngOnInit() {
    const user = localStorage.getItem('user');
    if (user != null) {
      this.userDetails = JSON.parse(user);
    }
    this.getByUserId(this.userDetails.id);
  }

  private getByUserId(id: any) {
    const url = '/rest/user/' + id;
    return this.httpService.get(url).subscribe((res: any) => {
      if (res.code != '0000') {
        this.modalService.error('查询失败');
      } else {
        this.userInfo.id = res.data.id;
        this.userInfo.email = res.data.email;
        if (res.data.email != null && res.data.email != '') {
          if (res.data.emailActivated == 0) {
            this.userInfo.emailActivated = 1;
          }
          if (res.data.emailActivated == 1) {
            this.userInfo.emailActivated = 2;
          }
        }
        this.userInfo.name = res.data.name;
        this.userInfo.lastLogin = res.data.lastLogin;

        this.userInfo.lastLoginIP = res.data.lastLoginIP;
      }
    });
  }

  handleCancel() {
    this.showUpdateFlag = false;
  }

  showResetPassword() {
    this.showUpdateFlag = true;
  }

  resetPassword() {
    const url = '/rest/user/password/reset';
    if (this.resetInfo.oldPassword == this.resetInfo.newPassword) {
      this.modalService.error('新旧密码不能一样！');
      return;
    }
    if (this.resetInfo.confirmPassword != this.resetInfo.newPassword) {
      this.modalService.error('新密码两次输入不一致！');
      return;
    }
    const data = {
      id: this.userDetails.id,
      password: this.resetInfo.oldPassword,
      newPassword: this.resetInfo.newPassword,
    };
    return this.httpService.post(url, data).subscribe((res: any) => {
      if (res.code != '0000') {
        this.modalService.error(res.message);
      } else {
        this.modalService.success('密码修改成功');
        this.showUpdateFlag = false;
      }
    });
  }

  showVerification() {
    this.userInfo.verificationCode = '';
    this.showVerificationFlag = true;
  }

  showVerificationCancel() {
    this.showVerificationFlag = false;
  }

  sendVerification() {
    this.sendButton = true;
    this.count = 60;
    const url = '/rest/verify/email/generate';
    const data = {
      id: this.userDetails.id,
      email: this.userInfo.email,
    };
    return this.httpService.post(url, data).subscribe((res: any) => {
      if (res.code != '0000') {
        this.modalService.error(res.message);
      } else {
        this.modalService.success('验证码发送成功！');
        this.timer = setInterval(() => {
          if (this.count > 0) {
            this.count = this.count - 1;
            this.countText =
              '(' + this.count.toString() + '秒后重新获取验证码)';
          }
          if (this.count == 0) {
            this.sendButton = false;
            this.countText = '';
            clearInterval(this.timer);
          }
        }, 1000);
      }
    });
  }

  activeEmail() {
    const url = '/rest/verify/email/active';
    const data = {
      id: this.userInfo.id,
      code: this.userInfo.verificationCode,
      email: this.userInfo.email,
    };
    return this.httpService.post(url, data).subscribe((res: any) => {
      if (res.code != '0000') {
        this.modalService.error(res.message);
      } else {
        this.modalService.success('邮箱激活成功！');
        this.showVerificationFlag = false;
        this.getByUserId(this.userInfo);
        location.reload();
      }
    });
  }
}
